//
//  RestfullAPI.swift
//  BaseProj
//
//  Created by 陈宇 on 2017/4/26.
//  Copyright © 2017年 科匠武汉. All rights reserved.
//

import Foundation
import Moya

enum RestFullAPI {
    // 用户
    case Registe(username: String, password: String)
    case Login(username: String, password: String)
    case Info
    case UpdateInfo(member: Member)
    
    // 房屋
    case Houses
    case FavoriteHouses
    case FeaturedHouses
    case PostedHouses
    case HouseInfo(houseId: String)
    case Favorite(houseId: String)
    case UnFavorite(houseId: String)
    
    // 竞标
    case BidList(state: BiddingType)
}

extension RestFullAPI: TargetType {
    var baseURL: URL {
        return URL(string: APP.API_HOST)!
    }
    
    var path: String {
        switch self {
        case .Registe:
            return "/v1/member"
        case .Login:
            return "/v1/member/login"
        case .Info:
            return "/v1/member/info"
        case .UpdateInfo:
            return "/v1/member"
            
        case .Houses:
            return "/v1/houses"
        case .FavoriteHouses:
            return "/v1/houses/favorite"
        case .FeaturedHouses:
            return "/v1/houses/featured"
        case .PostedHouses:
            return "/v1/houses/post"
        case .HouseInfo(let id):
            return "/v1/houses/\(id)"
        case .Favorite:
            return "/v1/houses/favorite"
        case .UnFavorite:
            return "/v1/houses/un_favorite"
            
        case .BidList(let state):
            return "/v1/bid/list/\(state.apiIndex())"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .Registe:
            return .post
        case .Login:
            return .post
        case .Info:
            return .get
        case .UpdateInfo:
            return .put
            
        case .Houses:
            return .get
        case .FavoriteHouses:
            return .get
        case .FeaturedHouses:
            return .get
        case .PostedHouses:
            return .get
        case .HouseInfo:
            return .get
        case .Favorite:
            return .post
        case .UnFavorite:
            return .post
            
        case .BidList:
            return .get
        }
    }
    
    var parameters: [String : Any]? {
        switch self {
        case .Registe(let username, let password):
            return ["username": username, "password": password]
        case .Login(let username, let password):
            return ["username": username, "password": password]
        case .UpdateInfo(let member):
            return member.toJSON()
            
        case .Houses:
            return nil
        case .Favorite(let id):
            return ["id": id]
        case .UnFavorite(let id):
            return ["id": id]
            
        default:
            return nil
        }
    }
    
    var parameterEncoding: Moya.ParameterEncoding {
        return JSONEncoding.default
    }
    
    var sampleData: Data {
        return Data()
    }
    
    var task: Moya.Task {
        return .request
    }
}

extension RestFullAPI {
    var requiredToken: Bool {
        switch self {
        case .Registe, .Login:
            return false
        default:
            return true
        }
    }
}

extension RestFullAPI: NetworkSpinnerable {
    var shouldDisplay: Bool {
        switch self {
//        case .Registe, .Login:
//            return true
        default:
            return false
        }
    }
}
